//
//  AppDelegate.swift
//  crkWeibo
//
//  Created by 陈荣科 on 15/12/15.
//  Copyright © 2015年 crk. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //加载账号
        let userAccount = CZUserAccount.loadUserAccount()
        
        print("userAccount:\(userAccount)")
        
        
        window = UIWindow(frame:UIScreen.mainScreen().bounds)
        
//        let tabBar = CZMainViewController()
        
           let navBar = UINavigationBar.appearance()
           navBar.tintColor = UIColor.orangeColor()
    
        
        window?.rootViewController = defaultViewController()
        
        //测试欢迎界面
//        window?.rootViewController = CZWelcomeViewController()
        
//        window?.rootViewController = CZNewfeatureViewController()
        
        //测试判断是否为新版本
//       let newVersion = isNewVersion()
//    
//        print("newVersion:\(newVersion)")
        window?.makeKeyAndVisible()
        window?.backgroundColor = UIColor.whiteColor()
        
        return true
    }
    //MARK: - 程序启动后的默认界面
    private func defaultViewController() -> UIViewController{
    
        //判断用户是否登录
        if !CZUserAccount.userLogin{
        
            //用户没有登录，返回CZMainViewController对象
            return CZMainViewController()
        }
        //用户已登录
        //判断是否是新版本
        return isNewVersion() ? CZNewfeatureViewController():CZWelcomeViewController()

    }

    //MARK: -切换界面 有两个界面切换，
     /// 切换程序的根控制器, isMain = true 表示切换到MainViewController, isMain = false 表示切换到WelcomeViewController
    func switchRootViewController(isMain: Bool) {
    
        window?.rootViewController = isMain ? CZMainViewController() : CZWelcomeViewController()
    }
    
    
  //MARK: - 判断是否是新版本
  /// 判断是否是新版本
    private func isNewVersion() -> Bool {
    
        //1.获取当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        print("当前版本:\(currentVersion)")
       
        //2.获得上次保存的版本
        let sandboxVersionKey = "sandboxVersionKey" //保存到沙盒的key
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        
        print("沙盒版本:\(sandboxVersion)")
        
        //3.保存当前版本
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandboxVersionKey)
        //同步
        NSUserDefaults.standardUserDefaults().synchronize()
        
        //4.返回，当前版本与上一次版本的比较值
        return currentVersion > sandboxVersion
    }

}

